package com.common.library.tools;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;

/**
 * @author txf
 * @Title app相关  文件保存路劲  版本号 版本名称 等.
 * @package com.common.library.tools
 * @date 2017/6/1 0001
 */

public class AppInfo {
    private Context appContext = null;
    private static AppInfo appInfo;

    private AppInfo() {
    }

    /**
     * 保证在application中执行了init - 否则context无效
     *
     * @return
     */
    public static AppInfo getInstance() {
        if (appInfo == null)
            appInfo = new AppInfo();
        return appInfo;
    }

    public void init(Context context) {
        appContext = context;
    }

    /**
     * 获取文件保存路劲
     */
    public String getStoragePath() {
        String path = null;
        if (!StringTools.isNull(appContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()))
            path = appContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        else if (!StringTools.isNull(appContext.getFilesDir().getAbsolutePath()))
            path = appContext.getFilesDir().getAbsolutePath();
        return path;
    }

    /**
     * 获取友盟渠道
     * */
    public String getChannel() {
        if (appContext == null) {
            return null;
        }
        String channel = null;
        try {
            PackageManager packageManager = appContext.getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.
                        getApplicationInfo(appContext.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channel = String.valueOf(applicationInfo.metaData.get("UMENG_CHANNEL"));
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            return "";
        }
        return channel;
    }
}
